package com.licq.tv.data;

import com.licq.tv.ResourceTable;

import com.licq.tv.bean.BaseBean;
import com.licq.tv.bean.JokeBean;
import com.licq.tv.net.MobileApi;
import com.licq.tv.net.NetInheritObserver;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import retrofit2.Call;

import java.util.ArrayList;

public class ListHolder {
    private static final String TAG = "ListHolder";

    private AbilitySlice mSlice;

    private ComponentContainer mRootLayout;

    private ListContainer listContainer;

    private ListItemProvider listItemProvider;

    public ListHolder(AbilitySlice abilitySlice) {
        mSlice = abilitySlice;
        listItemProvider = new ListItemProvider(abilitySlice);
    }

    public ComponentContainer createComponent() {
        Component mainComponent = LayoutScatter.getInstance(mSlice).parse(ResourceTable.Layout_ability_main, null, false);
        if (!(mainComponent instanceof ComponentContainer)) {
            return null;
        }
        mRootLayout = (ComponentContainer) mainComponent;
        listContainer = (ListContainer) mRootLayout.findComponentById(ResourceTable.Id_list);
        listContainer.setItemProvider(listItemProvider);
        MobileApi.getInstance().getJoke(1,20,"video")
                .enqueue(new NetInheritObserver<BaseBean<ArrayList<JokeBean>>>(mSlice.getContext()){
                    @Override
                    public void onSubscribed(Call disposable) {

                    }

                    @Override
                    public void onSuccess(BaseBean<ArrayList<JokeBean>> arrayListBaseBean, String errorMsg) {
                        StringBuilder stringBuilder = new StringBuilder();
                        for (JokeBean jokeBean : arrayListBaseBean.getResult()){
                            HiLog.debug(new HiLogLabel(HiLog.DEBUG,1,"licq"),jokeBean.getName());
                            stringBuilder.append(jokeBean.getName());
                        }
                        listItemProvider.setData(arrayListBaseBean.getResult());
//                componentById.setText(stringBuilder.toString());
                    }

                    @Override
                    public void onFail(String msg) {
                        HiLog.debug(new HiLogLabel(HiLog.DEBUG,1,"licq"),msg);
                    }

                });
        return mRootLayout;
    }
}
